﻿using ImTools;
using Prism.Ioc;
using SECSGEMServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZDevice.Event;
using ZZMES.Data;
using ZZMES.TCP;

namespace ZZMES.Process
{
    public class ProcessGeneral : ProcessBase
    {
        public ProcessGeneral(IEventAggregator eventAggregator, IContainerProvider containerProvider, TCPHostService tcpHostService, DataProviderService dataProviderService) : base(eventAggregator, containerProvider, tcpHostService, dataProviderService)
        {
            eventAggregator.GetEvent<TcpEvent>().Subscribe(OnDeviceStateChanged);   //设备发送状态改变
            eventAggregator.GetEvent<TcpEvent>().Subscribe(OnAlarm);               //设备发送报警
            eventAggregator.GetEvent<SECSEvent>().Subscribe(OnReceiveMessage);     //主机发送消息
            eventAggregator.GetEvent<TcpEvent>().Subscribe(OnReceipeChanged);      //设备发送配方改变
            //eventAggregator.GetEvent<TcpEvent>().Subscribe(OnSendMessage); 
            eventAggregator.GetEvent<SECSEvent>().Subscribe(OnAcquireReceipe);        //主机请求配方列表
            eventAggregator.GetEvent<TcpEvent>().Subscribe(OnSendPPList);             //设备发送配方列表
            eventAggregator.GetEvent<SECSEvent>().Subscribe(OnDeleteReceipe);        //主机请求删除配方
        }


        /// <summary>
        /// 设备发送状态改变
        /// </summary>
        /// <param name="args"></param>
        private void OnDeviceStateChanged(TcpEventArgs args)
        {
            if (args.msg.Contains("S1F1")) 
            {
                string deviceName = GetDeviceName(args.client);
                if (deviceName != null) 
                {
                    string[] info = args.msg.Split(":"); 
                    var service = serviceContainer.Resolve<KingSecsGemService>(deviceName);
                    service.SendDeviceState((DeviceState)int.Parse(info[1]));
                }
                
            }
        }

        /// <summary>
        /// 设备报警
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnAlarm(TcpEventArgs args)
        {
            if (args.msg.Contains("S3F1"))
            {
                string deviceName = GetDeviceName(args.client);
                if (deviceName != null)
                {
                    string[] info = args.msg.Split(":");
                    string[] childInfo = info[1].Split(',');
                    var service = serviceContainer.Resolve<KingSecsGemService>(deviceName);
                    service.SendAlarm(int.Parse(childInfo[0]), true);
                }

            }
        }

        //private void OnSendMessage(TcpEventArgs args)
        //{
        //    if (args.msg.Contains("S3F1"))
        //    {
        //        string deviceName = GetDeviceName(args.client);
        //        if (deviceName != null)
        //        {
        //            string[] info = args.msg.Split(":");
        //            string[] childInfo = info[1].Split(',');
        //            var service = serviceContainer.Resolve<KingSecsGemService>(deviceName);
        //            service.
        //        }

        //    }
        //}

        /// <summary>
        /// 主机端发送消息
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnReceiveMessage(SECSEventArgs args)
        {
            if(args.EventType == EventType.Message) 
            {
                var conn = dataProviderService.communicateConfigs.FirstOrDefault(c => c.Name == DeviceNames[args.id]);
                if(conn!=null) 
                {
                    string client = conn.IP+":"+conn.Port;
                    tcpHostService.Send(client, args.msg);
                }
                
            }
        }

        /// <summary>
        /// 配方改变
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnReceipeChanged(TcpEventArgs args)
        {
            if (args.msg.Contains("S5F1"))
            {
                string deviceName = GetDeviceName(args.client);
                if (deviceName != null)
                {
                    string[] info = args.msg.Split(":");
                    string[] childInfo = info[1].Split(',');
                    var service = serviceContainer.Resolve<KingSecsGemService>(deviceName);
                    service.SendPPChanged(int.Parse(childInfo[0]), childInfo[1]);
                }

            }
        }

        /// <summary>
        /// 主机请求配方列表
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnAcquireReceipe(SECSEventArgs args)
        {
            if (args.EventType == EventType.PPlist)
            {
                var conn = dataProviderService.communicateConfigs.FirstOrDefault(c => c.Name == DeviceNames[args.id]);
                if (conn != null)
                {
                    string client = conn.IP + ":" + conn.Port;
                    tcpHostService.Send(client, "S5F2");
                }

            }
        }

        /// <summary>
        /// 设备发送配方列表
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnSendPPList(TcpEventArgs args)
        {
            if (args.msg.Contains("S5F3"))
            {
                string deviceName = GetDeviceName(args.client);
                if (deviceName != null)
                {
                    string[] info = args.msg.Split(":");
                    string[] childInfo = info[1].Split(',');
                    var service = serviceContainer.Resolve<KingSecsGemService>(deviceName);
                    service.SendPPList(0, childInfo);
                }

            }
        }

        private void OnDeleteReceipe(SECSEventArgs args)
        {
            if (args.EventType == EventType.DeletePP)
            {
                var conn = dataProviderService.communicateConfigs.FirstOrDefault(c => c.Name == DeviceNames[args.id]);
                if (conn != null)
                {
                    string client = conn.IP + ":" + conn.Port;
                    string pps="";
                    foreach (var item in args.msgs)
                    {
                        pps += item;
                        pps+= ",";
                    }
                    tcpHostService.Send(client, "S5F4:"+ args.msgs);
                }

            }
        }

        internal string GetDeviceName(string client) 
        {
            string[] info = client.Split(":");
            var conn = dataProviderService.communicateConfigs.Find(x=>x.IP == info[0]);
            if(conn == null)
                return null;
            else
                return conn.Name;
        }
    }
}
